<?php

namespace app\admin\model\fastflow\bill;


use app\admin\model\Admin;
use think\Config;
use think\Model;
use fastflow\api;

class Common extends Model
{

    // 追加属性
    protected $append = [
        'initiator',
        'bill',
        'flow_auth',
        'runthread_info',
        'flow_progress',
        'flow_auth_text',
    ];

    public function __construct($data = [])
    {
        parent::__construct($data);
        if(isset($this->appendAttr))
        {
            $this->append = array_unique(array_merge($this->append,$this->appendAttr));
        }
    }

    public function getName()
    {
        return $this->name;
    }

    public function getInitiatorAttr($value, $data)
    {
        $createUserId = $data['initiator_id'];
        $user = Admin::find($createUserId);
        if ($user) {
            return $user['nickname'];
        }
        return '';
    }

    public function getBillAttr($value, $data)
    {
        return Config::get('database')['prefix'] . $this->name;
    }

    public function getFlowAuthAttr($value, $data)
    {
        $bill = Config::get('database')['prefix'] . $this->name;
        return (new api())->getBillAuthInfo($data['initiator_id'], $bill, $data['id']);
    }

    public function getFlowAuthTextAttr($value, $data)
    {
        return $this->getFlowAuthAttr($value, $data)['msg'];
    }

    public function getRunthreadInfoAttr($value, $data)
    {
        $bill = Config::get('database')['prefix'] . $this->name;
        return (new api())->getRunThreadInfo($bill, $data['id']);
    }

    public function getFlowProgressAttr($value, $data)
    {
        $bill = Config::get('database')['prefix'] . $this->name;
        return (new api())->getFlowProgress($bill, $data['id']);
    }
}